package com.cube.dp.adapter;

import com.cube.dp.adapter.fs.custom.IFileOperationService;
import com.cube.dp.adapter.fs.third.IThirdPartyFileOperationService;
import com.cube.dp.adapter.fs.third.dto.FileUploadDto;
import com.cube.dp.adapter.fs.third.dto.FileUploadForStreamDto;

/**
 * @author litb
 * @date 2022/5/21 16:52
 * <p>
 * 抽象文件双写适配器,文件上传时,会先将文件上传只自研文件服务实现,然后再将该文件上传至第三方文件服务实现,
 * 同一个文件上传后在自研文件服务和第三方文件服务内的文件id是一致的
 */
public abstract class AbstractFileDoubleWriteOperationAdaptor implements FileOperationAdaptee {

    private final IFileOperationService fileOperationService;

    private final IThirdPartyFileOperationService thirdPartyFileOperationService;

    public AbstractFileDoubleWriteOperationAdaptor(IFileOperationService fileOperationService,
                                                   IThirdPartyFileOperationService thirdPartyFileOperationService) {
        this.fileOperationService = fileOperationService;
        this.thirdPartyFileOperationService = thirdPartyFileOperationService;
    }

    @Override
    public String upload(FileUploadDto dto) {
        String fileId = fileOperationService.upload(to(dto));
        //使用上传至自研文件服务的文件id作为第三方文件服务上传文件时的key,这样就能够报证同一个文件在两个文件服务的文件id是一致的,这个逻辑这里就不写了
        return thirdPartyFileOperationService.upload(dto);
    }

    @Override
    public String upload4Stream(FileUploadForStreamDto streamDto) {
        String fileId = fileOperationService.upload4Stream(to(streamDto));
        //使用上传至自研文件服务的文件id作为第三方文件服务上传文件时的key,这样就能够报证同一个文件在两个文件服务的文件id是一致的,这个逻辑这里就不写了
        return thirdPartyFileOperationService.upload4Stream(streamDto);
    }

    public IFileOperationService getFileOperationService() {
        return fileOperationService;
    }

    public IThirdPartyFileOperationService getThirdPartyFileOperationService() {
        return thirdPartyFileOperationService;
    }
}
